//-----------------------------------------------------------------------
// <copyright file="CreateKey.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Jim Hartill</author>
// <email>jameshar@microsoft.com</email>
// <date>2004-07-12</date>
// <summary>Creates a Registry Key in a specified hive on a specified machine.</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.Registry
{
    using System;
    using System.Reflection;
    using System.Collections;
    using System.Runtime.InteropServices;
    using System.Security;

    using Microsoft.Sdc.Tasks;
    using Microsoft.Win32;
    using Microsoft.Build.Framework;

    /// <summary>
    /// 
    /// </summary>
    public class CreateKey : TaskBase
    {
        private string machineName;
        private string registryHive;
        private string key;

        [Required]
        public string RegistryHive
        {
            get
            {
                return (registryHive == null ? String.Empty : registryHive);
            }

            set
            {
                registryHive = value;
            }
        }

        /// <summary>
        /// Defaults to local machine if not specified.
        /// </summary>
        /// <value></value>
        public string MachineName
        {
            get
            {
                return (machineName == null ? String.Empty : machineName);
            }
            set
            {
                machineName = value;
            }
        }

        [Required]
        public string Key
        {
            get
            {
                return (key == null ? String.Empty : key);
            }

            set
            {
                key = value;
            }
        }

        /// <summary>
        /// </summary>
        public CreateKey()
        {
        }


        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            RegistryKey registryKey = null;
            Microsoft.Win32.RegistryHive hive = (Microsoft.Win32.RegistryHive)Enum.Parse(typeof(Microsoft.Win32.RegistryHive), this.registryHive, true);
            try
            {
                registryKey = RegistryKey.OpenRemoteBaseKey(hive, this.MachineName).CreateSubKey(this.Key);
            }
            catch(ArgumentException)
            {
                throw new TaskException("Registry.Argument", Key, MachineName);
            }
            catch(SecurityException)
            {
                throw new TaskException("Registry.Security", Key);
            }
            registryKey.Close();
        }
    }
}
